package com.nbjtjc.safe.service.sys;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysGroupMapper;
import com.nbjtjc.safe.mapper.sys.SysOptionGroupMapper;
import com.nbjtjc.safe.model.sys.SysGroup;
import com.nbjtjc.safe.model.sys.SysOptionGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysOptionGroupService {

    @Autowired
    private SysOptionGroupMapper optionGroupMapper;

    @Autowired
    private SysGroupMapper groupMapper;

    public List<SysOptionGroup> find(String groupName) {
        SysOptionGroup sysOptionGroup=new SysOptionGroup();
        SysGroup sysGroup =new SysGroup();
        sysGroup.setGroupname(groupName);
        sysGroup=groupMapper.selectOne(sysGroup);
        if(ObjectUtil.isNull(sysGroup)){
            return new ArrayList<>();
        }
        sysOptionGroup.setGroupid(sysGroup.getId());
        List<SysOptionGroup> groups = optionGroupMapper.search(sysOptionGroup);
        return groups;
    }

    public Result add(SysOptionGroup sysOptionGroup) {
        Result result = new Result<>();
        if (ObjectUtil.isNull(sysOptionGroup)) {
            result.setFalse("插入不能为空");
            return result;
        }
        int res = optionGroupMapper.insert(sysOptionGroup);
        if (res == 0) {
            result.setFalse("从插入失败");
            return result;
        }
        result.setData(sysOptionGroup);
        return result;
    }

    public Result update(SysOptionGroup sysOptionGroup) {
        Result result = new Result<>();
        if (ObjectUtil.isNull(sysOptionGroup) || ObjectUtil.isNull(sysOptionGroup.getId())) {
            result.setFalse("更新不能为空");
            return result;
        }
        int res = optionGroupMapper.updateByPrimaryKeySelective(sysOptionGroup);
        if (res == 0) {
            result.setFalse("更新失败");
            return result;
        }
        SysOptionGroup temp=new SysOptionGroup();
        temp.setId(sysOptionGroup.getId());
        result.setData(optionGroupMapper.search(temp).get(0));
        return result;
    }

    @Transactional(rollbackFor = {Throwable.class})
    public Result addOptionGroup(SysGroup sysGroup,List<Integer> ids) {
        Result result = new Result<>();
        int res = optionGroupMapper.deleteByGroupId(sysGroup.getId());
        if (CollectionUtil.isNotEmpty(ids)&&ids.size() > 0) {
            List<SysOptionGroup> optionGroups = new ArrayList<>();
            int num=1;
            for (Integer optionId : ids) {
                optionGroups.add(new SysOptionGroup( sysGroup.getId(), optionId,num++));
            }
            optionGroupMapper.insertList(optionGroups);
            result.setData(optionGroupMapper.search(new SysOptionGroup(sysGroup.getId())));
        }
        return result;
    }
}
